/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.*;

/**
 * <p>Project: app2022 - Exam11
 * <p>Powered by webrx On 2022-01-20 15:07:03
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Exam11 {
    public static void main(String[] args) {
        File f1 = new File("D:\\aa4");
        File f2 = new File("c:\\useraaa");


        //将f1目录复制到f2目录下
        //d:\\aa4\\*  复制到 d:\\aa999\\aa4\\*
        copedir(f1, f2);
    }

    private static void copefile(File f1, File f2) {
        if (f1.isFile()) {
            try (FileInputStream fis = new FileInputStream(f1);
                 FileOutputStream fos = new FileOutputStream(f2)) {
//            方法一
//                byte[] bytes = fis.readAllBytes();
//                fis.close();
//                fos.write(bytes);
//            方法二
                fis.transferTo(fos);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void copedir(File f1, File f2) {
        if (f1.isDirectory()) {
            String name = f1.getName();
            File f3 = new File(f2, name);
            if (!f3.exists()) {
                f3.mkdirs();
            }
            for (File f : f1.listFiles()) {
                copedir(f, f3);
            }
        } else {
            File f3 = new File(f2, f1.getName());
            copefile(f1, f3);
        }
    }
}
